package org.itboys.mobile.service.mysql.front;

import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.promotion.SundayCouponActivityMapper;
import org.itboys.mobile.dao.promotion.SundayCouponMapper;
import org.itboys.mobile.dto.SundayCouponDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.promotion.SundayCoupon;
import org.itboys.mobile.entity.mysql.promotion.SundayCouponActivity;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mysql.service.BaseMySqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by zhangyang on 2017/12/20.
 * 圣代_优惠券_service
 */
@Service
public class SundayFrontCouponService extends BaseMySqlService<SundayCoupon>{

    @Autowired
    private SundayCouponMapper couponMapper;
    @Autowired
    private SundayCouponActivityMapper couponActivityMapper;
    @Autowired
    private SundayMemberService memberService;


    /**
     * 获取用户的优惠券
     * @param memberId  用户id
     * @param amount    金额
     * @return
     */
    public List<SundayCouponDto> select(Long memberId, BigDecimal amount){
        Map<String,Object> param= new HashMap<String,Object>();
        param.put("memberId",memberId);
        //查询满足条件的优惠券
        if(amount != null){
            param.put("status",SundayCommonConstants.coupon_status_wsy);
            param.put("baseAmount_smaller",amount);
        }
        List<SundayCoupon> coupons = super.select(param);

        //组装dto
        List<SundayCouponDto> list = new ArrayList<SundayCouponDto>();
        for(SundayCoupon coupon : coupons){
            SundayCouponDto dto = new SundayCouponDto();
            dto.setBaseAmount(coupon.getBaseAmount().setScale(0, BigDecimal.ROUND_HALF_UP).toString());
            dto.setDecuAmount(coupon.getDecuAmount().setScale(0,BigDecimal.ROUND_HALF_UP).toString());
            dto.setEndTime(TimeUtils.format(coupon.getEndTime(),"yyyy/MM/dd"));
            dto.setStatus(coupon.getStatus());
            dto.setCreateDate(coupon.getCreateDate());
            dto.setType(coupon.getType());
            dto.setStartTime(TimeUtils.format(coupon.getCt(),"yyyy/MM/dd"));
            list.add(dto);
        }
        return  list;
    }

    /**
     * 获取最优的一张优惠券
     * @param memberId  用户id
     * @param amount    金额
     * @return  优惠券dto
     */
   /* public SundayCoupon getOptimalCoupon(Long memberId, BigDecimal amount){
        Map<String,Object> param= new HashMap<String,Object>();
        param.put("memberId",memberId);
        param.put("status",SundayCommonConstants.coupon_status_wsy);
        //查询满足条件的优惠券
        if(amount != null){
            param.put("baseAmount_smaller",amount);
        }
        List<SundayCoupon> coupons = super.select(param);
        //2018年1月21日，此方法内部使用不需要返回DTO
        if(ListUtil.isNotNull(coupons)){
            return  coupons.get(0);
        }
        //组装dto
        *//*if(ListUtil.isNotNull(coupons)){
            SundayCouponDto dto = new SundayCouponDto();
            SundayCoupon coupon = coupons.get(0);
            dto.setBaseAmount(coupon.getBaseAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            dto.setDecuAmount(coupon.getDecuAmount().setScale(2,BigDecimal.ROUND_HALF_UP).toString());
            dto.setEndTime(TimeUtils.format(coupon.getEndTime(),"yyyy-MM-dd"));
            dto.setStatus(coupon.getStatus());
            dto.setCreateDate(coupon.getCreateDate());
            dto.setType(coupon.getType());
            return  dto;
        }*//*
        return null;
    }
*/

    /**
     * 查询最优的一张优惠券，2018年1月21日，江雄
     * @param memberId
     * @param amount
     * @return
     */
    public SundayCoupon getOptimalCoupon(Long memberId, BigDecimal amount){
        Map<String,Object> param= new HashMap<String,Object>();
        param.put("memberId",memberId);
        param.put("status",SundayCommonConstants.coupon_status_wsy);
        param.put("baseAmount_smaller",amount);
        //按启用金额倒叙找出
        param.put("sort_name","baseAmount");
        param.put("sort_order","DESC");
        List<SundayCoupon> coupons = super.select(param);
        if(ListUtil.isNotNull(coupons))return coupons.get(0);

        return null;
    }

    /**
     *发放优惠券
     * @param memberId  用户id
     * @param type  类型:1，注册赠送。2，系统定时发送。配合发送时间使用
     */
    public void sendCoupon(Long memberId,Integer type){
        Map<String ,Object> param=new HashMap<String,Object>();
        SundayMember member=memberService.getById(memberId);
        param.put("type", type);
        param.put("status", SundayCommonConstants.YES);
        //查询所有有效的
        List<SundayCouponActivity> activities=couponActivityMapper.select(param);
        if(ListUtil.isNotNull(activities)){
            //当前时间
            Date now=new Date();
            Integer currentDate=now.getDate();
            for(SundayCouponActivity activity:activities){
                //生成优惠券的失效时间
                now.setDate(currentDate+activity.getDay());
                SundayCoupon coupon=new SundayCoupon(activity.getId(),activity.getType(),member.getId(),member.getNickName(),activity.getBaseAmount(),activity.getDecuAmount(),
                        SundayCommonConstants.coupon_status_wsy,now);
                couponMapper.insert(coupon);
                now.setDate(currentDate);
            }
        }
    }

    /**
     * 使用优惠券
     * @param couponId
     */
    public void usedCoupon(Long couponId,Long memberId){
        SundayCoupon coupon = new SundayCoupon();
        coupon.setId(couponId);
        coupon.setStatus(SundayCommonConstants.coupon_status_ysy);
        super.update(coupon);
    }
}
